<?php

include 'db.php';

class Led{

    public $fd;
    public $speeds=[];

    function __construct($port,$baudRate)
    {
        $this->fd = conn_com(false, $port,$baudRate) or exit($port ." port not open\r\n");
        $cmd = pack('H*',bin2hex("DNSLED\x00\x02"));
        dio_write($this->fd,$cmd); //开屏
        $this->writeStr('.');
    }

    function strToGBK($strText)
    {
        $encode = mb_detect_encoding($strText, array('UTF-8','GB2312','GBK'));
        if($encode == "UTF-8")
        {
            return @iconv('UTF-8','GB18030',$strText);
        }
        else
        {
            return $strText;
        }
    }

    function getBinStr($str){
        $showtext =   $this->strToGBK($str);
        $slen = sprintf("%02x",strlen($showtext)+8);
        $cmd = "444e534c4544001f{$slen}0000000000010031c9cfd2c633303a".bin2hex($showtext);
        $msg= pack('H*',$cmd);
        return $msg;
    }

    function writeStr($str){
        $text = $this->getBinStr($str);
        dio_write($this->fd,$text);
    }

    function formatCont($d){
        $tmp = sprintf("%.1f",$d['temperature']);
        if($d['roller_layer_id']==0){

            $speed = (float)$d['speed'] *1000/60;
            $this->speeds[]= $speed;
            if(count($this->speeds)>9){
                array_shift($this->speeds);
            }

            $speed = array_sum($this->speeds)/count($this->speeds);

            if($speed<=0.3) $speed=0;
            if($speed>2.1) {
                $speed-=0.25;
            }

            $speed=sprintf("%.1f", $speed );
            $speed.='m/min';
        }else{
            $speed=sprintf("%.1f",$d['speed']);
            $speed.='km/h';
        }

        if($d['temperature']==0){
    	   $tmp.='   ';
    	}
        echo $text='温度'.$tmp.'℃ 速度'.$speed;
        return $text;
    }
}


function conn_com($fd ,$com_name,$baud=9600,$data = 8,$stop = 1)
{
    if($fd) return $fd;

    if(stripos(php_uname(),'windows')>-1){
        try {
            @exec("mode $com_name: baud=$baud data=$data stop=$stop parity=n xon=on");
            $fd = dio_open("$com_name:",O_RDWR);
        } catch(Exception $e) {
            echo "$com_name conn field \r\n";
            return false;
        }
        return $fd;
    }else{
        try{
            $fd = @dio_open($com_name, O_RDWR | O_NOCTTY | O_NONBLOCK);
            if(!$fd) return false;
            //dio_fcntl($fd, F_SETFL, O_SYNC);
            dio_tcsetattr($fd, array(
                'baud' => $baud,
                'bits' => 8,
                'stop'  => 1,
                'parity' => 0
            ));
        } catch(Exception $e){
            echo "$com_name conn field \r\n";
            return false;
        }
    }

    echo "$com_name success\r\n";
    return $fd;
}


function getLedPort(){
    $cmd = 'ls -l /dev/ttyUSB*|sed -n \'$p\'|awk \'{print $10}\'';
    exec($cmd ,$result,$status);
    return $result[0];
}

$confstr= file_get_contents('/home/wwwroot/pavgps/config.json');
$conf= json_decode($confstr,true);
//$conf['LedSerialPort'] = getLedPort(); 
//$led = new Led('/dev/ttyUSBZ2',9600);
//$led->writeStr($led->formatCont('hahaha'));

if(!$conf['LedSerialPort']){
    exit('not set LedSerialPort');	
}

$redisc = new Redis();
$redisc->connect('119.3.254.184',6888);
$redisc->auth('zjzd');

$led = new Led('/dev/ttyS1',38400);

$redisc->subscribe(['order'], function ($redisc, $chan, $msg) {
    var_dump($msg);
});


